package com.kawa.kawa_ad.handler;

import android.app.Activity;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.splashad.api.ATSplashAd;
import com.anythink.splashad.api.ATSplashAdExtraInfo;
import com.anythink.splashad.api.ATSplashAdListener;
import com.kawa.kawa_ad.util.Utils;

import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;

public class SplashHandler implements ATSplashAdListener {
    private final Activity mActivity;
    private EventChannel.EventSink mEventSink;
    private final ATSplashAd mSplashAd;
    private FrameLayout mContent;
    private final BinaryMessenger mMessenger;

    public SplashHandler(Activity activity, BinaryMessenger messenger, String splashId){
        mActivity = activity;
        mMessenger = messenger;
        mSplashAd = new ATSplashAd(activity,splashId,this);
    }

    private void setupEventSink(int channelId){
        EventChannel mEventChannel = new EventChannel(mMessenger, "com.mob.adsdk/event_" + channelId);
        mEventChannel.setStreamHandler(new EventChannel.StreamHandler() {
            @Override
            public void onListen(Object o, EventChannel.EventSink eventSink) {
                mEventSink = eventSink;
            }

            @Override
            public void onCancel(Object o) {
                mEventSink = null;
            }
        });
    }

    public void load(int channelId){
        setupEventSink(channelId);
        mSplashAd.loadAd();
    }

    public void show(int channelId,String scene){
        setupEventSink(channelId);
        if (mSplashAd.isAdReady()){
            if (mContent == null){
                mContent = new FrameLayout(mActivity);
                mActivity.addContentView(mContent, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
            }
            mSplashAd.show(mActivity,mContent,scene);
        }else if (mSplashAd.checkAdStatus().isLoading()){
            Map<String,Object> map = new HashMap<>();
            map.put("name","showLoading");
            map.put("result","0");
            mEventSink.success(map);
            closeSplash();
        }else{
            Map<String,Object> map = new HashMap<>();
            map.put("name","showFail");
            map.put("result","1");
            mEventSink.success(map);
            closeSplash();
            mSplashAd.loadAd();
        }
    }

    private void closeSplash(){
        if (mContent != null) {
            mContent.removeAllViews();
            ((ViewGroup) mContent.getParent()).removeView(mContent);
            mContent = null;
        }
        if (mEventSink != null) {
            mEventSink.endOfStream();
            mEventSink = null;
        }
    }

    @Override
    public void onAdLoaded(boolean b) {
        Map<String,Object> map = new HashMap<>();
        if (!b){
            map.put("name","loaded");
            map.put("result","0");
        }else {
            map.put("name","fail");
            map.put("result","1");
        }
        mEventSink.success(map);
        closeSplash();
    }

    @Override
    public void onAdLoadTimeout() {
        Map<String,Object> map = new HashMap<>();
        map.put("name","timeout");
        map.put("result","0");
        mEventSink.success(map);
        closeSplash();
        mSplashAd.loadAd();
    }

    @Override
    public void onNoAdError(AdError adError) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","fail");
        map.put("result","1");
        map.put("error",adError.getDesc());
        mEventSink.success(map);
        closeSplash();
    }

    @Override
    public void onAdShow(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","show");
        map.put("result","0");
        map.put("info",Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
    }

    @Override
    public void onAdClick(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","click");
        map.put("result","0");
        map.put("info", Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
    }

    @Override
    public void onAdDismiss(ATAdInfo atAdInfo, ATSplashAdExtraInfo atSplashAdExtraInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","dismiss");
        map.put("result","0");
        map.put("info", Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
        closeSplash();
        mSplashAd.loadAd();
    }
}
